package com.aisile.admin.service.imp;

import com.aisile.admin.mapper.AdChannelMapper;
import com.aisile.admin.service.AdChannelService;
import com.sun.deploy.util.StringUtils;


@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper,AdChannel> implements AdChannelService {
    /**
     * 分页和搜索 根据名称进行 模糊查询
     * @param dto 用来存储分页数据 页数  总页数  和 搜索的neritic
     * @return
     */
    @Override
    public ResponseResult getPageList(ChannelDto dto) {
        System.out.println(dto);
        Page<AdChannel> pagelist = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<AdChannel> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(!StringUtils.isEmpty(dto.getName()),AdChannel::getName,dto.getName());
        IPage<AdChannel> pageinfo = this.page(pagelist, wrapper);
        return ResponseResult.okResult(pageinfo);
    }

    /**
     * service 添加方法
     * @param dto 添加的数据
     * @return
     */
    @Override
    public ResponseResult addAdmin(AdChannel dto) {
        if (dto.getName()==null){
            return ResponseResult.okResult(2001,"频道名称不能为空!");
        }
        if (dto.getIsDefault()==null){
            dto.setIsDefault(false);
        }
        if (dto.getStatus()==null){
            dto.setStatus(false);
        }
        this.save(dto);

        return ResponseResult.okResult(2000,"添加成功");
    }

    /**
     * 根据id去修改 status 数据
     * @param id
     * @param status
     * @return
     */
    @Override
    public ResponseResult updatestatus(int id, Boolean status) {
        //先去表中查询 id对应的该数据
        AdChannel adChannel = this.getById(id);
        adChannel.setStatus(status);
        if (status==null){
            return ResponseResult.okResult(2001,"该值不能为空");
        }
        this.updateById(adChannel);

        return ResponseResult.okResult(2000,"修改成功");
    }

    /**
     * 修改方法
     * @param id
     * @param dto 存储修改的数据
     * @return
     */
    @Override
    public ResponseResult updateAdmin(int id, AdChannel dto) {
        AdChannel channel = this.getById(id);
        dto.setId(channel.getId());
        if (dto==null ||dto.getName()==null||dto.getId()==null){
            return ResponseResult.okResult(2001,"缺少参数!");
        }

        this.updateById(dto);
        return ResponseResult.okResult(2000,"修改成功!");
    }

    @Override
    public ResponseResult getlist(Integer id) {

        if (id==null){
            return ResponseResult.okResult(2001,"缺少参数!");
        }
        AdChannel list = this.getById(id);
        return ResponseResult.okResult(list);
    }
}
